package cn.lyjuan.aliyun.mq.conf.java.conf;

import cn.lyjuan.aliyun.mq.conf.config.RootConfig;
import cn.lyjuan.aliyun.mq.conf.java.test.TestUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

import java.util.Date;
import java.util.UUID;

/**
 * junit每个单元测试的方法都会启动不同的线程，所以需要共享的变量需要加 static（甚至 volatile）
 */
public abstract class BaseTestConf
{
    private static Logger log = LogManager.getLogger(BaseTestConf.class);

    @Autowired
    protected ApplicationContext applicationContext;

    protected DefaultListableBeanFactory beanFactory;

    protected String PRODUCER_NAME;

    protected String CONSUMER_NAME;

    protected final String BODY = "HelloTest";

    protected final String KEY = new Date().getTime() + "";

    protected String TAG = TestUtils.rdmTag();

    protected String proFilePath;

    @Before
    public void before()
    {
        // 测试时，文件不能重复，测试用例间会相互删除
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        proFilePath = RootConfig.class.getClassLoader().getResource(".").getPath() + "/cst/mqUserCst-" + uuid + ".properties";

        beanFactory = (DefaultListableBeanFactory) applicationContext.getAutowireCapableBeanFactory();
        PRODUCER_NAME = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 10) + "P";
        CONSUMER_NAME = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 10) + "C";

        if (log.isDebugEnabled())
            log.debug("run test before: {}, key: {}, producer: {}, consumer: {}, proFilePath: {}",
                    this.hashCode(), KEY, PRODUCER_NAME, CONSUMER_NAME, proFilePath);
    }
}
